#!/bin/bash

### Generate Qt/C++ classes from template files.

DIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd )"
TMPL_DIR=${DIR}/templates
MINIMUM_ARGS=3
ERROR_GENERAL=1
ERROR_BADARGS=65

### Functions
print_usage() {

    echo "Usage: `basename $0` plain|qobject class_name out_dir"
}

generate_class () {
    CLASS_NAME=$1
    OUT_DIR=$2
    CLASS_NAME_UPPER=$(echo "${CLASS_NAME}" | tr '[:lower:]' '[:upper:]')
    CLASS_NAME_LOWER=$(echo "${CLASS_NAME}" | tr '[:upper:]' '[:lower:]')

    cat ${HEADER_TMPL} | sed -e "s/%{class_name}/${CLASS_NAME}/g" \
                             -e "s/%{class_name.upper}/${CLASS_NAME_UPPER}/g" \
                             -e "s/%{class_name.lower}/${CLASS_NAME_LOWER}/g" \
                         > ${OUT_DIR}/${CLASS_NAME_LOWER}.h

    cat ${IMPL_TMPL}   | sed -e "s/%{class_name}/${CLASS_NAME}/g" \
                             -e "s/%{class_name.upper}/${CLASS_NAME_UPPER}/g" \
                             -e "s/%{class_name.lower}/${CLASS_NAME_LOWER}/g" \
                         > ${OUT_DIR}/${CLASS_NAME_LOWER}.cpp
}

### Main
if [ "$#" -lt "$MINIMUM_ARGS" ]
then
  print_usage
  exit $ERROR_BADARGS
fi

case $1 in
    plain)
        HEADER_TMPL=${TMPL_DIR}/header.tmpl
        IMPL_TMPL=${TMPL_DIR}/implementation.tmpl
        ;;

    qobject)
        HEADER_TMPL=${TMPL_DIR}/header-qobject.tmpl
        IMPL_TMPL=${TMPL_DIR}/implementation-qobject.tmpl
        ;;
esac

generate_class $2 $3
